package com.devo.icecream.ui.home

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.material.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp


@Composable
fun TestScreen(
    log: String = "",
    result: String = "",
    address: String = "",
    onAddress: (String) -> Unit = {},
    sendInt: (Int) -> Unit = {},
    sendFloat: (Float) -> Unit = {},
    read: () -> Unit = {},
) {
    val intCmd = remember {
        mutableStateOf("")
    }
    val floatCmd = remember {
        mutableStateOf("")
    }

    val addressValue = remember(address) {
        mutableStateOf(address)
    }

    val scrollState = rememberScrollState(0)

    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        Column(
            modifier = Modifier
                .padding(32.dp)
                .height(300.dp)
                .background(Color.Yellow)
                .verticalScroll(state = scrollState)
        ) {
            Text(
                modifier = Modifier
                    .fillMaxSize(),
                text = log
            )
        }

        TextField(
            modifier = Modifier
                .padding(32.dp)
                .fillMaxWidth(),
            colors = TextFieldDefaults.textFieldColors(
                backgroundColor = Color.Transparent
            ),
            leadingIcon = @Composable {
                Text(text = "地址(比如 9004)：")
            },
            value = addressValue.value,
            onValueChange = onAddress,
        )


        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 32.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            TextField(
                modifier = Modifier.weight(1f),
                colors = TextFieldDefaults.textFieldColors(
                    backgroundColor = Color.Transparent
                ),
                leadingIcon = @Composable {
                    Text(text = "Int：")
                },
                value = intCmd.value,
                onValueChange = { intCmd.value = it },
            )

            Button(onClick = { sendInt(intCmd.value.toInt()) }) {
                Text(text = "写入")
            }

        }
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 32.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            TextField(
                modifier = Modifier.weight(1f),
                colors = TextFieldDefaults.textFieldColors(
                    backgroundColor = Color.Transparent
                ),
                leadingIcon = @Composable {
                    Text(text = "Float：")
                },
                value = floatCmd.value,
                onValueChange = { floatCmd.value = it },
            )

            Button(onClick = { sendFloat(floatCmd.value.toFloat()) }) {
                Text(text = "写入")
            }

        }

        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 32.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            TextField(
                modifier = Modifier.weight(1f),
                colors = TextFieldDefaults.textFieldColors(
                    backgroundColor = Color.Transparent
                ),
                leadingIcon = @Composable {
                    Text(text = "结果：")
                },
                readOnly = true,
                value = result,
                onValueChange = { },
            )

            Button(onClick = { read() }) {
                Text(text = "读取")
            }

        }


    }
}

@Preview(showBackground = true)
@Composable
fun TestPreview() {
    val str = (0..1000).fold("") { a, v -> a + v }.toList().joinToString("")

    TestScreen(log = str);
}